package com.example.tool.widget.event;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.LinearLayout;

import com.example.tool.util.DebugLog;

import static com.example.tool.util.DebugLog.EVENT_VIEW;

/**
 * 描述:
 *1、如果ViewGroup找到了能够处理该事件的View，则直接交给子View处理，自己的onTouchEvent不会被触发；
 *
 * 2、可以通过复写onInterceptTouchEvent(ev)方法，拦截子View的事件（即return true），把事件交给自己处理，则会执行自己对应的onTouchEvent方法
 *
 * 3、子View可以通过调用getParent().requestDisallowInterceptTouchEvent(true);  阻止ViewGroup对其MOVE或者UP事件进行拦截；
 * @author: liangkun
 * @date: 2019/11/22 14:29
 */
public class MyLinearLayout extends LinearLayout {


    public MyLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        int action = ev.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                DebugLog.w(EVENT_VIEW + "MyLinearLayout dispatchTouchEvent ACTION_DOWN");
                break;
            case MotionEvent.ACTION_MOVE:
                DebugLog.w(EVENT_VIEW + "MyLinearLayout dispatchTouchEvent ACTION_MOVE");
                break;
            case MotionEvent.ACTION_UP:
                DebugLog.w(EVENT_VIEW + "MyLinearLayout dispatchTouchEvent ACTION_UP");
                break;

            default:
                break;
        }
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        int action = event.getAction();

        switch (action) {
            case MotionEvent.ACTION_DOWN:
                DebugLog.w(EVENT_VIEW + "MyLinearLayout onTouchEvent ACTION_DOWN");
                break;
            case MotionEvent.ACTION_MOVE:
                DebugLog.w(EVENT_VIEW + "MyLinearLayout onTouchEvent ACTION_MOVE");
                break;
            case MotionEvent.ACTION_UP:
                DebugLog.w(EVENT_VIEW + "MyLinearLayout onTouchEvent ACTION_UP");
                break;

            default:
                break;
        }

        return super.onTouchEvent(event);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
//        默认是不拦截的，即返回false；如果你需要拦截，只要return true就行了，
//        这要该事件就不会往子View传递了，
//        并且如果你在DOWN retrun true，则DOWN,MOVE,UP子View都不会捕获事件；[子控件申请消费都不行]
//    如果你在MOVE return true , 则子View在MOVE和UP都不会捕获事件。

        int action = ev.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                DebugLog.w(EVENT_VIEW + "MyLinearLayout onInterceptTouchEvent ACTION_DOWN");
                break;
//                return true; //拦截 返回true
            case MotionEvent.ACTION_MOVE:
                DebugLog.w(EVENT_VIEW + "MyLinearLayout onInterceptTouchEvent ACTION_MOVE");
//                break;
                return true; //拦截 返回true
            case MotionEvent.ACTION_UP:
                DebugLog.w(EVENT_VIEW + "MyLinearLayout onInterceptTouchEvent ACTION_UP");
                break;
//                return true; //拦截 返回true

            default:
                break;
        }

        return false;
    }

    @Override
    public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
        DebugLog.w(EVENT_VIEW + "MyLinearLayout requestDisallowInterceptTouchEvent ");
        super.requestDisallowInterceptTouchEvent(disallowIntercept);
    }

}
